package com.feng.chat.controller;

import com.feng.chat.dto.Result;
import com.feng.chat.dto.UserDTO;

import com.feng.chat.service.IUsersService;
import com.feng.chat.utils.GetUserUtil;
import com.feng.chat.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/api/v1")
public class UserController {
    @Autowired
    private IUsersService usersService;

    /**
     * 用户登录
     *
     * @return
     */
    @PostMapping("/auth/login")
    public Result login(HttpServletRequest request,@RequestBody UserDTO userDTO) {
        return usersService.login(request,userDTO);
    }

    /**
     * 用户注册
     *
     * @param userDTO
     * @return
     */
    @PostMapping("/auth/register")
    public Result register(@RequestBody UserDTO userDTO) {
        Result result = usersService.register(userDTO);

        return result;
    }

    /**
     * 获取用户基础信息及界面信息
     *
     * @param request
     * @return
     */
    @GetMapping("/users/setting")
    public Result usersSetting(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Integer id = JwtUtil.parserToken(token);
        Result result = usersService.usersSetting(id);
        return result;
    }

    /**
     * 个人详细信息
     *
     * @param request
     * @return
     */
    @GetMapping("/users/detail")
    public Result usersDetail(HttpServletRequest request) {
        Integer userId = GetUserUtil.getUserId(request);
        Result result = usersService.usersDetail(userId);
        return result;
    }

    /**
     * 修改个人信息
     *
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/users/change/detail")
    public Result changeDetail(HttpServletRequest request, @RequestBody Map<String, String> map) {
        Integer userId = GetUserUtil.getUserId(request);
        Result result = usersService.changeDetail(userId, map.get("avatar"), Integer.parseInt(map.get("gender")), map.get("motto"), map.get("nickname"));
        return result;
    }

    /**
     * 修改密码
     *
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/users/change/password")
    public Result changePassword(HttpServletRequest request, @RequestBody Map<String, String> map) {
        Integer userId = GetUserUtil.getUserId(request);
        Result result = usersService.changePassword(userId, map.get("new_password"), map.get("old_password"));
        return result;
    }

    /**
     * 绑定邮箱
     *
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/users/change/email")
    public Result changeEmail(HttpServletRequest request, @RequestBody Map<String, String> map) {
        HttpSession session = request.getSession();
        Integer userId = GetUserUtil.getUserId(request);
        Result result = usersService.changeEmail(userId, map.get("email"), map.get("email_code"), map.get("password"), session);
        return result;
    }

    /**
     * 绑定手机
     *
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/users/change/mobile")
    public Result changeMobile(HttpServletRequest request, @RequestBody Map<String, String> map) {
        Integer userId = GetUserUtil.getUserId(request);
        HttpSession session = request.getSession();
        Result result = usersService.changeMobile(userId, map.get("mobile"), map.get("password"), map.get("sms_code"), session);
        return result;
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @PostMapping("/auth/logout")
    public Result logout(HttpServletRequest request){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = usersService.logout(userId);
        return result;
    }
}
